package com.cweb.admin.model.sys.sys_config.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cweb.admin.model.sys.sys_config.entity.SysConfig;
import com.cweb.admin.model.sys.sys_config.mapper.SysConfigMapper;
import com.cweb.framework.base.serivce.BaseMybatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统-系统配置表 服务实现类
 *
 * @author changfan
 * @since 2019-04-23
 */
@Service
public class SysConfigService extends BaseMybatisService<SysConfigMapper, SysConfig> {
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 取得全部配置
     */
    @Cacheable(value = "config", key = "targetClass + 'config'")
    public JSONObject getConfig() {
        QueryWrapper param = new QueryWrapper();
        param.eq("status", "0");
        // 取得全量
        List<SysConfig> listSysConfig = this.list(param);
        // 返回值
        JSONObject retJson = new JSONObject();
        for (SysConfig sysConfig : listSysConfig) {
            if (StrUtil.isBlank(sysConfig.getKey()) || StrUtil.isBlank(sysConfig.getValue())) {
                continue;
            }
            retJson.put(sysConfig.getKey(), sysConfig.getValue());
        }
        return retJson;
    }

    public String getConfigStr(String key) {
        JSONObject retJson = sysConfigService.getConfig();
        if (null != retJson && retJson.containsKey(key)) {
            return retJson.getStr(key);
        }
        return null;
    }

    /**
     * 删除配置缓存
     */
    @CacheEvict(value = "config", key = "targetClass + 'config'")
    public void delConfig() {
    }

    /*
     * 配置设置
     */
    public void updateConfig(String key, String value) {
        QueryWrapper param = new QueryWrapper();
        param.eq("`key`", key);
        SysConfig sysConfig = this.getOne(param);
        if (null != sysConfig) {
            sysConfig.setValue(value);
            this.updateById(sysConfig);
        }
    }

    /*
     * 查询
     */
    public List<SysConfig> processSelect(SysConfig param) {
        return this.list(new QueryWrapper<>(param));
    }

    /*
     * 新增
     */
    public SysConfig processAdd(SysConfig param) {
        this.save(param);
        // 刷新缓存
        sysConfigService.delConfig();
        return param;
    }

    /*
     * 更新
     */
    public SysConfig processChange(SysConfig param) {
        SysConfig sysConfig = this.getById(param.getId());
        sysConfig.setName(param.getName());
        sysConfig.setKey(param.getKey());
        sysConfig.setValue(param.getValue());
        sysConfig.setType(param.getType());
        this.updateById(sysConfig);
        // 刷新缓存
        sysConfigService.delConfig();
        return sysConfig;
    }

    /*
     * 删除
     */
    public SysConfig processDelete(SysConfig param) {
        this.removeById(param.getId());
        // 刷新缓存
        sysConfigService.delConfig();
        return param;
    }
}
